package com.itboye.ihomebank.activity.finance;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.CurrentDaiKuanBean;
import com.itboye.ihomebank.bean.PhoneInfo;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.LoweRelativeLayout;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.GetPhoneNumberFromMobile;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.UIAlertView;
import com.itboye.ihomebank.web.WebActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import static com.itboye.ihomebank.util.DoubleUtil.getDouble;

/**
 * 我要还款
 */
public class HuanKuanActivity extends BaseOtherActivity implements Observer {

    Button btn_huakuan;
    TextView txt_title, txt_right, huankuan2, huankuan3;
    ImageView img_back;
    JinRongPresenter jinRongPresenter;
    String uid;
    RelativeLayout nodata;
    LoweRelativeLayout hasData;
    private CurrentDaiKuanBean.HuanKUanDetailBean currentDaiKuanBean;
    MyApplcation myApp;
    Button btn_jiekuan;
    private GetPhoneNumberFromMobile getPhoneNumberFromMobile;
    private List<PhoneInfo> list = new ArrayList<PhoneInfo>();

    private UIAlertView uiAlertView;
    @Override
    protected int layout_id() {
        return R.layout.activity_huan_kuan;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    public void refreshData(){
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        jinRongPresenter = new JinRongPresenter(this);
        showProgressDialog("正在获取数据,请稍后",true);
        jinRongPresenter.currentJieKuan(uid);
    }

    @Override
    protected void onInit() {
        setHasData(false);
        myApp = (MyApplcation) getApplication();
        myApp.huanKuanUI = this;
        txt_title.setText(getIntent().getStringExtra("title"));
        txt_right.setText("借款记录");

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.txt_right:
                intent = new Intent(this, JieKuanRecordActivity.class);
                intent.putExtra("type", 0);//1为借款
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_huakuan:
                intent = new Intent(this, JieKuanRecordActivity.class);
                intent.putExtra("type", 1);//0为还款
                intent.putExtra("item_id", currentDaiKuanBean.getId());
                break;
            case R.id.btn_jiekuan:
                intent = new Intent(this, WebActivity.class);
                intent.putExtra("url", NetPublicConstant.WEB_URL02+"/loan/instruction");
                break;
        }
        if (intent != null) {
            startActivity(intent);
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (!entity.getCode().equals("0")) {
                ByAlert.alert(entity.getMsg());
                setHasData(false);
                return;
            }
            if (entity.getEventType() == JinRongPresenter.currentJieKuan_success) {
                currentDaiKuanBean = (CurrentDaiKuanBean.HuanKUanDetailBean) entity.getData();
                if (currentDaiKuanBean != null) {
                    if (currentDaiKuanBean.getId() == 0) {
                        setHasData(false);
                        return;
                    }
                    setHasData(true);
                    huankuan2.setText(getDouble(currentDaiKuanBean.getTotal_money() + "") + "元");
                    huankuan3.setText("下期还款  ¥" + DoubleUtil.doubleTransform(currentDaiKuanBean.getTotal())+ "元");
                }
            } else if (entity.getEventType() == JinRongPresenter.currentJieKuan_fail) {
                ByAlert.alert(entity.getData());
            }else if (entity.getEventType() == JinRongPresenter.tongxunlu_success) {//设置通讯录
                ByAlert.alert(entity.getData());
            }else if (entity.getEventType() == JinRongPresenter.tongxunlu_fail) {
                ByAlert.alert(entity.getData());
            }
        }

    }

    private void setHasData(boolean is) {
        if (!is) {
            hasData.setVisibility(View.GONE);
            nodata.setVisibility(View.VISIBLE);
            btn_huakuan.setVisibility(View.GONE);
        } else {
            hasData.setVisibility(View.VISIBLE);
            nodata.setVisibility(View.GONE);
            btn_huakuan.setVisibility(View.VISIBLE);

            //当第一次登陆还款时，读取通讯录
            if ((SPUtils.get(this, null, SPContants.HUANKUAN, "") + "").equals("1")){
                uiAlertView = new UIAlertView(this, "是否住家访问您的通讯录？","手机通讯录", "是", "取消");
                uiAlertView.show();
                uiAlertView.setClicklistener(new UIAlertView.ClickListenerInterface() {
                    @Override
                    public void doLeft() {
                        uiAlertView.dismiss();

                        SPUtils.put(HuanKuanActivity.this, null, SPContants.HUANKUAN,"2");

                        getPhoneNumberFromMobile = new GetPhoneNumberFromMobile();
                        list = getPhoneNumberFromMobile.getPhoneNumberFromMobile(HuanKuanActivity.this);

                        StringBuffer sb=new StringBuffer();//PhoneInfo
                        for (int i = 0; i < list.size(); i++) {
                            sb.append(list.get(i).getName()+":"+list.get(i).getNumber()+";");
                        }
                      //  showProgressDialog("正在设置,请稍后",true);
                        jinRongPresenter.tongXunLu(uid,sb.substring(0,sb.length()-1));
                    }
                    @Override
                    public void doRight() {
                        uiAlertView.dismiss();
                        SPUtils.put(HuanKuanActivity.this, null, SPContants.HUANKUAN,"2");
                    }
                });
            }
        }
    }

}
